// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved.


/****************************************************************************
						Microsoft RPC
          
                      cxhndl Example

    FILE:       cxhndlc.c

    USAGE:      cxhndlc  -n network_address
                         -p protocol_sequence
                         -e endpoint
                         -a server principal name
                         -o options
                         -f filename

    PURPOSE:    Client side of RPC distributed application

    FUNCTIONS:  main() - binds to server and calls remote procedure

    COMMENTS:   This distributed application uses a context handle.

****************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include "cxhndl.h"    // header file generated by MIDL compiler
#include "spn.h"


#define PURPOSE \
"This Microsoft RPC Version 2.0 sample program demonstrates\n\
the use of the [context_handle] attribute. For more information\n\
about attributes and RPC API functions, see the RPC programming\n\
guide and reference.\n\n"


void Usage(char * pszProgramName)
{
    fprintf_s(stderr, "%s", PURPOSE);
    fprintf_s(stderr, "Usage:  %s\n", pszProgramName);
    fprintf_s(stderr, " -p protocol_sequence\n");
    fprintf_s(stderr, " -n network_address\n");
    fprintf_s(stderr, " -e endpoint\n");
    fprintf_s(stderr, " -a server principal name\n");	
    fprintf_s(stderr, " -o options\n");
    fprintf_s(stderr, " -f filename\n");
    exit(1);
}

void __cdecl main(int argc, char **argv)
{
    RPC_STATUS status;
    PCONTEXT_HANDLE_TYPE phContext = NULL;

    unsigned char * pbBuf = NULL;
    short cbRead;    // count of bytes read

    unsigned char * pszUuid             = NULL;
    unsigned char * pszProtocolSequence = "ncacn_ip_tcp";
    unsigned char * pszNetworkAddress   = NULL;
    unsigned char * pszEndpoint         = "8765";
    unsigned char * pszSpn              = NULL;	
    unsigned char * pszOptions          = NULL;
    unsigned char * pszStringBinding    = NULL;
    unsigned char * pszFileName         = "readme.txt";
	RPC_SECURITY_QOS SecQos;
    int i;

    /* allow the user to override settings with command line switches */
    for (i = 1; i < argc; i++) {
        if ((*argv[i] == '-') || (*argv[i] == '/')) {
            switch (tolower(*(argv[i]+1))) {
            case 'p':  // protocol sequence
                pszProtocolSequence = argv[++i];
                break;
            case 'n':  // network address
                pszNetworkAddress = argv[++i];
                break;
            case 'e':
                pszEndpoint = argv[++i];
                break;
            case 'a':  
                pszSpn = argv[++i];
                break;
            case 'o':
                pszOptions = argv[++i];
                break;
            case 'f':
                pszFileName = argv[++i];
                break;
            case 'h':
            case '?':
            default:
                Usage(argv[0]);
            }
        }
        else
            Usage(argv[0]);
    }

    pbBuf = (unsigned char *)
            midl_user_allocate(BUFSIZE * sizeof(unsigned char));

    /* Use a convenience function to concatenate the elements of  */
    /* the string binding into the proper sequence.               */
    status = RpcStringBindingCompose(pszUuid,
                                     pszProtocolSequence,
                                     pszNetworkAddress,
                                     pszEndpoint,
                                     pszOptions,
                                     &pszStringBinding);
    printf_s("RpcStringBindingCompose returned 0x%x\n", status);
    printf_s("pszStringBinding = %s\n", pszStringBinding);
    if (status) {
        exit(status);
    }

    /* Set the binding handle that will be used to bind to the server. */
    status = RpcBindingFromStringBinding(pszStringBinding,
                                         &hStarter);
    printf_s("RpcBindingFromStringBinding returned 0x%x\n", status);
    if (status) {
	        exit(status);
    }

   /* User did not specify spn, construct one. */
   if (pszSpn == NULL) {
	   MakeSpn(&pszSpn);
   }
   
   /* Set the quality of service on the binding handle */
   SecQos.Version = RPC_C_SECURITY_QOS_VERSION_1;
   SecQos.Capabilities = RPC_C_QOS_CAPABILITIES_MUTUAL_AUTH;
   SecQos.IdentityTracking = RPC_C_QOS_IDENTITY_DYNAMIC;
   SecQos.ImpersonationType = RPC_C_IMP_LEVEL_IDENTIFY;
   
   /* Set the security provider on binding handle */
   status = RpcBindingSetAuthInfoEx(hStarter,
									pszSpn,
									RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
									RPC_C_AUTHN_GSS_NEGOTIATE,
									NULL,
									RPC_C_AUTHZ_NONE,
									&SecQos);
   
   printf_s("RpcBindingSetAuthInfoEx returned 0x%x\n", status);
   if (status) {
	   exit(status);
   }
    
   RpcTryExcept{
    printf_s("Calling the remote procedure RemoteOpen\n");
    if (RemoteOpen(&phContext, pszFileName) < 0) {
        printf_s("Unable to open %s\n", pszFileName);
        Shutdown();
        exit(2);
    }
  }
   RpcExcept(( ( (RpcExceptionCode() != STATUS_ACCESS_VIOLATION) &&
                   (RpcExceptionCode() != STATUS_DATATYPE_MISALIGNMENT) &&
                   (RpcExceptionCode() != STATUS_PRIVILEGED_INSTRUCTION) &&
                   (RpcExceptionCode() != STATUS_BREAKPOINT) &&
                   (RpcExceptionCode() != STATUS_STACK_OVERFLOW) &&
                   (RpcExceptionCode() != STATUS_IN_PAGE_ERROR) &&
                   (RpcExceptionCode() != STATUS_GUARD_PAGE_VIOLATION)
                    )
                    ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH )){
	printf_s("Runtime reported exception \n");

	
	exit(1);
  }
  RpcEndExcept

    /* Now the context handle also manages the binding. */
    status = RpcBindingFree(&hStarter);
    printf_s("RpcBindingFree returned 0x%x\n", status);
    if (status) {
        exit(status);
    }

    /*  Free the string binding */
    status = RpcStringFree(&pszStringBinding);
    printf_s("RpcStringFree returned 0x%x\n", status);
    if (status) {
        exit(status);
    }

    printf_s("Calling the remote procedure RemoteRead\n");
    while (RemoteRead(phContext, pbBuf, &cbRead) > 0) {
        for (i = 0; i < cbRead; i++)
            putchar(*(pbBuf+i));
    }

    printf_s("Calling the remote procedure RemoteClose\n");
    if (RemoteClose(&phContext) < 0 ) {
        printf_s("Close failed on %s\n", pszFileName);
        exit(2);
    }
    
    exit(0);

}  // end main()


/*********************************************************************/
/*                 MIDL allocate and free                            */
/*********************************************************************/

void __RPC_FAR * __RPC_USER midl_user_allocate(size_t len)
{
    return(malloc(len));
}

void __RPC_USER midl_user_free(void __RPC_FAR * ptr)
{
    free(ptr);
}

/* end cxhndlc.c */
